 var App = getApp()
 //
 var WxParse = require('../../common/wxParse/wxParse.js');
 let uploadImage = require('../../../utils/uploadFile.js');
 // WxParse.wxParse('article', 'html', data.data.introduction, this, 5) // 解析html代码 富文本显示
 Page({
   /**
    * 页面的初始数据
    */
   data: {
     releaseFocus: true,
     page: 1,
     rows: 10,
     pinlun: [],
     imgList: [],
     modalName: null,
     isShow: false

   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
     console.log(options)
     this.setData({
       id: options.id,
     })
     if (options.did){
this.setData({
  xfc:1
})
     }
     var params = {
       articleId: options.id,
       buyerId: wx.getStorageSync('userId')
     }
     //获取导航数据
     App.HttpService.zzxq(params).then(res => {
       console.log(res)
       this.setData({
         nei: res.data.data.articleInfo,
         zuozhe: res.data.data.author,
         // 收藏
         isCollection: res.data.data.isCollection,
         // 点赞
         isSupport: res.data.data.isSupport

       })
       // 解析html代码 富文本显示
   
       WxParse.wxParse('article', 'html', res.data.data.articleInfo.content, this, 5)

     });

     // 请求评论

     this.pinglun();
   },
   // 进入回复页9面
   huifu: function(e) {
     console.log(e)

     var s = e.currentTarget.dataset.id
     wx.navigateTo({
       url: '../../merchants/huifu/huifu?id=' + s,
     })
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },


   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   // 图片上传
   ChooseImage() {
     var _this = this
     wx.chooseImage({
       count: 1, //默认9
       sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
       sourceType: ['album'], //从相册选择
       success: (res) => {
         var tempFilePaths = res.tempFilePaths;
         this.upload(tempFilePaths)
       }
     });
   },

   // 调用封装的上传图片的写法

   upload(img) {

     // 图片上传oss
     let that = this
     let {
       imgList
     } = that.data
     for (let i = 0; i < img.length; i++) {
       wx.showLoading({
         title: '上传中' + (i + 1) + '/' + img.length,
         mask: true
       })
       wx.hideLoading()
       uploadImage(img[i], 'weapp/', function(result) { // weapp/ 上传文件夹
         imgList.push(result)
         that.setData({
           imgList
         })
         wx.hideLoading()
       }, function(result) {
         wx.hideLoading()
       })
     }
   },

   ViewImage(e) {
     wx.previewImage({
       urls: this.data.imgList,
       current: e.currentTarget.dataset.url
     });
   },
   DelImg(e) {
     wx.showModal({
       title: '温馨提示',
       content: '确认删除图片',
       cancelText: '拒绝',
       confirmText: '确认',
       success: res => {
         if (res.confirm) {
           this.data.imgList.splice(e.currentTarget.dataset.index, 1);
           this.setData({
             imgList: this.data.imgList,
             tupian: ""
           })
         }

       }
     })
   },
   // 三元运算符有图片的时候
   ChooseImages() {

   },
   //点击发送
   fasong: function() {
 
     var params = {
       // 文章ID 或者评论id
       articleId: this.data.id,
       // 留言内容
       content: this.data.neirong,
       // 评论的类型 
       type: 0,
       // 当前评论人的id
       reviewPerson: wx.getStorageSync('userId'),
       // 图片路径
       path: this.data.imgList[0],
     }
     // 判断是否有输入的内容或者图片
     if (params.content || params.path) {
    
       if (params.content) {
         var paramss = {
           text: params.content
         }
       
         //验证敏感词汇
         App.HttpService.jiancem(paramss).then(res => {
           console.log(res, "李贺")
           var b = res.data.data
           if (b) {
         
             // 发送评论内容到后台
             App.HttpService.pingluns(params).then(res => {
               console.log(res)
               if (res.data.code == 200) {
                 console.log("评论成功")
                 wx.showToast({
                   title: '评论成功',
                   icon: 'success',
                   duration: 800
                 })
                 this.setData({
                   neirong: "",
                   imgList: [],
                   page: 1,
                   pinlun: []
                 })
              
                 this.pinglun();
               } else {
                 wx.showToast({
                   title: '评论失败',
                   icon: 'loading',
                   duration: 800
                 })
                 console.log("评论失败")
               }
             });
        
           } else {
             wx.showToast({
               title: '当前评论含有敏感词汇',
               icon: 'none',
               duration: 1800
             })
             return
           }
         });
       } else {
         // 发送评论内容到后台
         App.HttpService.pingluns(params).then(res => {
           console.log(res)
           if (res.data.code == 200) {
             console.log("评论成功")
             wx.showToast({
               title: '评论成功',
               icon: 'success',
               duration: 800
             })
             this.setData({
               neirong: "",
               imgList: [],
               page: 1,
               pinlun: []
             })
         
             this.pinglun();
           } else {
             wx.showToast({
               title: '评论失败',
               icon: 'loading',
               duration: 800
             })
             console.log("评论失败")
           }
         });
       }
     } else {
       wx.showToast({
         title: '内容不能为空',
         icon: 'loading',
         duration: 1000
       })
       return
     }
   },
   //
   // 获取内容
   neir: function(e) {
     console.log(e.detail.value)
     this.setData({
       neirong: e.detail.value
     })
   },
   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {
   },
   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {
     var s = this.data.page
     var z = this.data.zong
     if (z > s) {
       wx.showToast({
         title: '加载中',
         icon: 'loading',
         duration: 800
       })
       console.log("触底事件")
       this.setData({
         page: this.data.page + 1
       })
       this.pinglun();
     } else {
       console.log("到底了")
       wx.showToast({
         title: '到底了',
         icon: 'loading',
         duration: 800
       })

     }

   },

   // 点赞请求
   dianzan: function() {
     var params = {
       reviewId: this.data.nei.id,
       buyerId: wx.getStorageSync('userId')
     }
     //点赞请求
     App.HttpService.dianz(params).then(res => {
       console.log(res)
       this.setData({
         isSupport: res.data.data
       })
       if (res.data.data) {
         wx.showToast({
           title: '点赞成功',
           icon: 'success',
           duration: 800
         })
       } else {
         wx.showToast({
           title: '取消成功',
           icon: 'success',
           duration: 800
         })
       }

     })

   },





   //  收藏请求
   shoucang: function() {

     var params = {
       articleId: this.data.nei.id,
       buyerId: wx.getStorageSync('userId')
     }

     App.HttpService.wzsc(params).then(res => {
       console.log(res)
       this.setData({
         isCollection: res.data.data
       })
       if (res.data.data) {
         wx.showToast({
           title: '收藏成功',
           icon: 'success',
           duration: 800
         })
       } else {
         wx.showToast({
           title: '取消成功',
           icon: 'success',
           duration: 800
         })
       }

     })


   },



   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {
    
      return {
      title: "扳子养车",
        path: 'pages/merchants/zuxq/zuxq?id=' + this.data.id + "&did=" + 1,

      //这里设定都是以"/page"开头,并拼接好传递的参数
    }




   },
   /**
   * 点击回复
       releaseFocus: false,
   */
   bindReply: function(e) {
     this.setData({
       releaseFocus: true
     })
   },

   // 请求评论
   pinglun: function() {
     var id = this.data.id
     var params = {
       articleId: id,
       buyerId: wx.getStorageSync('userId'),
       page: this.data.page,
       rows: this.data.rows
     }
     App.HttpService.qqpl(params).then(res => {
       console.log(res, "评论")
       var datas = res.data.data.list

       this.setData({
         releaseFocus: !res.data.data.isShield,
         zong: res.data.data.totalPage,
         pinlun: this.data.pinlun.concat(datas)
       })
     })
   },
   // 点赞
   huifudianz: function(e) {
     console.log(e)
     // g改变单条里的数据
     var index = e.currentTarget.dataset.idx
     let arr = `pinlun[${index}].isSupport`;
     // 详情案例
     // let completeStatus = `todoLists[${index}].completeStatus`;
     this.setData({
       [arr]: !this.data.pinlun[index].isSupport,
     })
     var params = {
       reviewId: this.data.pinlun[index].id,
       buyerId: wx.getStorageSync('userId')
     }
     // 请求点赞的借口
     App.HttpService.pldz(params).then(res => {
       console.log(res)
       if (res.data.data) {
         let shu = `pinlun[${index}].supportCount`;
         this.setData({
           [shu]: this.data.pinlun[index].supportCount + 1,
         })
         wx.showToast({
           title: '点赞成功',
           icon: 'success',
           duration: 800
         })
       } else {
         let shu = `pinlun[${index}].supportCount`;
         this.setData({
           [shu]: this.data.pinlun[index].supportCount - 1,
         })
         wx.showToast({
           title: '取消成功',
           icon: 'success',
           duration: 800
         })
       }
     })
   },
//进入作者
   zuozhe: function(e) {
     console.log(e)
     var d = e.currentTarget.dataset.id

     wx.navigateTo({
       url: '/pages/zuozhe/zuozhe?id=' + d,
     })
   },
   dianjidd:function(){
     this.setData({
       isShow:!this.data.isShow
     })
   },
   jiang:function(){
     wx.switchTab({
       url: '/pages/index/index',
     })
   }



 })